package cn.itcast.bos.web.action.common;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;
import com.aliyuncs.exceptions.ClientException;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;


/**
 * Created by Think on 2017/9/21.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope(value = "prototype")
public class OrderAction extends BaseAction<Order>{

    private String sendAreaInfo;//寄件人信息
    private String recAreaInfo;//收件人信息

    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }

    @Action(value = "order_add",results = {@Result(name = SUCCESS,type = "redirect",location = "index.html")})
    public String add() throws ClientException {
//        System.out.println("进来了");
        //切割省市区信息,分开存储进区域中
        Area sendArea = new Area();
        String[] sendAreaData  = sendAreaInfo.split("/");
        sendArea.setProvince(sendAreaData[0]);
        sendArea.setCity(sendAreaData[1]);
        sendArea.setDistrict(sendAreaData[2]);

        Area recArea = new Area();
        String[] recAreaData = recAreaInfo.split("/");
        recArea.setProvince(recAreaData[0]);
        recArea.setCity(recAreaData[1]);
        recArea.setDistrict(recAreaData[2]);
        //把寄件人区域和收件人区域分别放进model对象中
        model.setSendArea(sendArea);
        model.setRecArea(recArea);

        //关联当前客户信息,向model存入客户id
        Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
        model.setCustomer_id(customer.getId());

        // 调用WebService 将数据传递 bos_management系统
        WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/orderService/order")
                .type(MediaType.APPLICATION_JSON)
                .post(model);

        return SUCCESS;
    }


}
